package com.yunji.hushuang.demo.controller;

import com.yunji.hushuang.demo.service.IDemoService;
import com.yunji.hushuang.framework.stereotype.HsAutowired;
import com.yunji.hushuang.framework.stereotype.HsController;
import com.yunji.hushuang.framework.web.annotation.HsRequestMapping;
import com.yunji.hushuang.framework.web.annotation.HsRequestMethod;
import com.yunji.hushuang.framework.web.annotation.HsRequestParam;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author hushuang
 * @since 2019/11/22
 */

@HsController
@HsRequestMapping("/demo")
public class DemoController {

    @HsAutowired
    private IDemoService demoService;

    @HsRequestMapping(value = "/", method = HsRequestMethod.GET)
    public String index() {
        return "welcome to hs-mvc";
    }

    @HsRequestMapping(value = "/plus", method = HsRequestMethod.GET)
    public String plus(@HsRequestParam("a") Integer a, @HsRequestParam("b") Integer b) {
        int result = demoService.plus(a, b);
        return String.format("plus: %d + %d = %d", a, b, result);
    }

    @HsRequestMapping(value = "/minus", method = HsRequestMethod.GET)
    public void minus(@HsRequestParam("a") Integer a, @HsRequestParam("b") Integer b, HttpServletResponse resp) throws IOException {
        int result = demoService.minus(a, b);
        String message = String.format("minus: %d - %d = %d", a, b, result);
        resp.getWriter().write(message);
    }
}
